<script setup name="Audit">
	import {ref, reactive} from 'vue'
	import {reimApi} from '@/api/reim.js'
	import {
		Edit,
		Search
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {useUserInfoStore} from "@/store/userInfo.js";
	import {deptApi} from "@/api/dept.js";
	//获取登录人员信息
	const userInfoStore = useUserInfoStore();
	const userLogin = ref({})
	userLogin.value = userInfoStore.getUserInfo()
	const loginUser = userInfoStore.getUserInfo().role
	//表格数据
	let selfAuditList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const reimQuery = reactive({
		page: 1,
		limit: 10,
		explain: ''
	})
	
	const loadData = () => {
		reimApi.selectByAdminId(reimQuery).then(result => {
			selfAuditList.value = result.data.list
			total.value = result.data.total
		})
	}
	
	loadData()
	
	const onSearch = () => {
		reimQuery.page = 1
		loadData()
	}
	
	//审批
	const dialogVisible = ref(false)
	const reim = ref({})
	const showUpdateDialog = (id) => {
		dialogVisible.value = true
		reim.value = {}
		reimApi.selectById(id).then(result => {
			console.log('报销单信息：' + result.data)
			reim.value = result.data
		})
	}
	
	const updateStatus = () => {
		reimApi.updateStatus(reim.value).then(result => {
			if (result.code === 0) {
				ElMessage({message: result.msg, type: 'success',})
				dialogVisible.value = false  //弹层消失
			} else {
				ElMessage.error(result.msg)
			}
			//刷新表格
			loadData()
		})
	}
	//条件搜索——部门下拉框
	const options = ref([])
	//获取部门数据
	const selectAllDept = () => {
		deptApi.selectAll().then(result=>{
			options.value = result.data
		})
	}
	selectAllDept()
	
	// 重置搜索
	const originData = () => ({explain: "", departmentId: "", beginCreateTime: "", endCreateTime: "",beginUpdateTime:"",endUpdateTime:""});
	const resetForm = () => {
		Object.assign(reimQuery, originData());
		loadData()
	};
</script>

<template>
	<div class="back">
		<el-card class="">
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="报销原因">
					<el-input v-model="reimQuery.explain" placeholder="请输入报销原因" clearable style='width:170px'/>
				</el-form-item>
				<el-form-item label="报销部门" v-if="loginUser== 1">
					<el-select v-model="reimQuery.departmentId" class="m-2"
							   clearable
							   placeholder="请选择部门"
							   @change='selectAllDept'
							   ar='clearSelect' style='width:170px'>
						<el-option
							v-for="item in options"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>
				<el-form-item label="申请时间" v-if="loginUser == 2">
					<el-date-picker
						v-model="reimQuery.beginCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
						style='width:175px'
					/>
					-
					<el-date-picker
						v-model="reimQuery.endCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
						style='width:175px'
					/>
				</el-form-item>
				<el-form-item label="审核时间" v-if="loginUser == 1">
					<el-date-picker
						v-model="reimQuery.beginUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
					/>
					-
					<el-date-picker
						v-model="reimQuery.endUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="selfAuditList" border stripe style="width: 100%;font-size:16px"
					  height="450px">
				<el-table-column prop="userName" label="报销人" align="center" min-width="110"/>
				<el-table-column prop="departName" label="报销部门" align="center" v-if="loginUser == 1" min-width="120"/>
				<el-table-column prop="totalAmount" label="总金额" align="center" sortable min-width="100"/>
				<el-table-column prop="alipayAccount" label="支付宝账号" align="center" min-width="150" show-overflow-tooltip/>
				<el-table-column prop="explain" label="报销说明" align="center" min-width="180" show-overflow-tooltip/>
				<el-table-column prop="image" label="附件证明" align="center" width="150" show-overflow-tooltip>
					<template #default="scope">
						<img :src="scope.row.image" style="max-height: 40px;max-width: 120px"/>
					</template>
				</el-table-column>
				<el-table-column prop="adminName" label="部门审核人" align="center" v-if="loginUser == 1" min-width="140" />
				<el-table-column prop="auditExplain" label="审核说明" align="center" v-if="loginUser == 1" min-width="160" />
				<el-table-column prop="status" label="审核状态" align="center" v-if="loginUser == 1" min-width="160" >
					<template #default="scope">
						<div v-if="scope.row.status == 1">部门通过</div>
						<div v-if="scope.row.status == 3">管理员申请</div>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="申请时间" align="center" v-if="loginUser == 2" min-width="220" sortable/>
				<el-table-column prop="updateTime" label="审核时间" align="center" v-if="loginUser == 1" min-width="230" sortable/>
				<el-table-column fixed="right" label="操作" min-width="120" align="center">
					<template #default="{row}">
						<el-button type="success" :icon="Edit" round @click="showUpdateDialog(row.id)">审批</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="reimQuery.page"
				v-model:page-size="reimQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
		
		<!--		审批弹出框-->
		<el-dialog v-model="dialogVisible" title="审批" width="500" :lock-scroll="false">
			<el-form :model="reim">
				<el-form-item label="报&nbsp;&nbsp;&nbsp;销&nbsp;&nbsp;&nbsp;人：" :label-width="100">
					<el-input v-model="reim.userName" autocomplete="off" disabled/>
				</el-form-item>
				<el-form-item label="报&nbsp;销&nbsp;部&nbsp;门：" v-if="loginUser == 1" :label-width="100">
					<el-input v-model="reim.departName" autocomplete="off" disabled/>
				</el-form-item>
				<el-form-item label="总&nbsp;&nbsp;&nbsp金&nbsp;&nbsp;&nbsp;额：" :label-width="100">
					<el-input v-model="reim.totalAmount" autocomplete="off" disabled/>
				</el-form-item>
				<el-form-item label="支付宝账号：" :label-width="100">
					<el-input v-model="reim.alipayAccount" autocomplete="off" disabled/>
				</el-form-item>
				<el-form-item label="审&nbsp;核&nbsp;人&nbsp;员：" v-if="loginUser== 1" :label-width="100">
					<el-input v-model="reim.adminName" autocomplete="off" disabled/>
				</el-form-item>
			</el-form>
			<el-form-item label="附&nbsp;件&nbsp;证&nbsp;明：" :label-width="100">
				<img v-if="reim.image" :src="reim.image" class="avatar" disabled=""/>
			</el-form-item>
			<el-form-item label="报&nbsp;销&nbsp;原&nbsp;因：" :label-width="100">
				<el-input v-model="reim.explain" type="textarea" disabled/>
			</el-form-item>
			<el-form-item label="审&nbsp;核&nbsp;说&nbsp;明：" :label-width="100">
				<el-input v-model="reim.auditExplain" type="textarea"/>
			</el-form-item>
			<el-form-item label="审&nbsp;核&nbsp;结&nbsp;果：" :label-width="100" v-if="loginUser == 2">
				<el-radio-group v-model="reim.status">
					<el-radio value="1" size="large" border>通过</el-radio>
					<el-radio value="0" size="large" border>不通过</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="审&nbsp;核&nbsp;结&nbsp;果：" :label-width="100" v-if="loginUser == 1">
				<el-radio-group v-model="reim.status">
					<el-radio value="2" size="large" border>通过</el-radio>
					<el-radio value="0" size="large" border>不通过</el-radio>
				</el-radio-group>
			</el-form-item>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="updateStatus">
						确认
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>

</template>

<style scoped>
	 .avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}
</style>